package com.gin.no_gay_alliance.fragment.post.attach;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.gin.no_gay_alliance.databinding.FragmentAttachmentBinding;
import com.gin.no_gay_alliance.databinding.MergeTabLayoutViewpager2ReverseBinding;
import com.gin.no_gay_alliance.exception.RequireParamException;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.Locale;

/**
 * 附件管理
 */
public class AttachmentFragment extends BaseBindingFragment<FragmentAttachmentBinding> implements TabLayoutMediator.TabConfigurationStrategy {
    TabLayoutMediator tabLayoutMediator;
    UploadFragment.Args uploadArgs;
    UploadedFragment.Args uploadedArgs;
    private ViewPager2 vp2;
    private TabLayout tabLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onCreateView() {
        if (getArguments() != null) {
            this.uploadArgs = getArguments().getParcelable(UploadFragment.Args.class.getName());
            this.uploadedArgs = getArguments().getParcelable(UploadedFragment.Args.class.getName());
        } else {
            throw new RequireParamException();
        }
        MergeTabLayoutViewpager2ReverseBinding merge = MergeTabLayoutViewpager2ReverseBinding.bind(b.getRoot());
        vp2 = merge.vp2;
        tabLayout = merge.tabLayout;

        vp2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                switch (position) {
                    case 0:
                        return UploadFragment.newInstance(uploadArgs);
                    case 1:
                        return UploadedFragment.newInstance(uploadedArgs);
                    default:
                        throw new RuntimeException("没有那个功能");
                }
            }

            @Override
            public int getItemCount() {
                return 2;
            }
        });


        // 绑定tabLayout和ViewPager2
        tabLayoutMediator = attachTabLayout(tabLayoutMediator, tabLayout, vp2);
    }

    @Override
    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
        switch (position) {
            case 0:
                tab.setText("上传附件");
                break;
            case 1:
                tab.setText(String.format(Locale.CHINA, "已有附件(%d)",
                        uploadedArgs.attachments == null ? 0 :
                                uploadedArgs.attachments.size()));
                break;
        }
    }

    public static AttachmentFragment newInstance(UploadFragment.Args uploadArgs, UploadedFragment.Args uploadedArgs) {
        AttachmentFragment fragment = new AttachmentFragment();
        Bundle args = new Bundle();
        args.putParcelable(UploadFragment.Args.class.getName(), uploadArgs);
        args.putParcelable(UploadedFragment.Args.class.getName(), uploadedArgs);
        fragment.setArguments(args);
        return fragment;
    }
}